package com.lqq.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lqq.commonutils.JwtUtils;
import com.lqq.commonutils.MD5;
import com.lqq.educenter.entity.UcenterMember;
import com.lqq.educenter.entity.vo.RegisterVo;
import com.lqq.educenter.mapper.UcenterMemberMapper;
import com.lqq.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lqq.servicebase.exceptionHandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author 677
 * @since 2021-07-09
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    //登录
    @Override
    public String login(UcenterMember member) {
        //获取手机号和密码
        String mobile = member.getMobile();//手机号
        String password = member.getPassword();//密码

        //1.先判断手机或密码是否为空
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001, "登录失败!");
        }

        //2.检验手机号
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);
        if (ucenterMember == null) {
            throw new GuliException(20001, "用户不存在，登录失败!");
        }

        //3.检验密码
        if (!MD5.encrypt(password).equals(ucenterMember.getPassword())) {
            throw new GuliException(20001, "密码错误，登录失败!");
        }

        //4.检验用户是否被禁用
        if (ucenterMember.getIsDisabled()) {
            throw new GuliException(20001, "登录失败!");
        }

        //5.生成token，jwt
        String jwtToken = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());
        return jwtToken;
    }

    //注册
    @Override
    public void register(RegisterVo registerVo) {
        //获取表单值
        String code = registerVo.getCode();//验证码
        String mobile = registerVo.getMobile();//手机号
        String nickname = registerVo.getNickname();//用户名
        String password = registerVo.getPassword();//密码

        //非空判断
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(mobile)
                || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001, "注册失败！");
        }

        //判断验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)) {
            throw  new GuliException(20001, "验证码错误,注册失败！");
        }

        //判断手机号是否存在（确保唯一）
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0) {
            throw new GuliException(20001, "用户已存在,注册失败！");
        }

        //数据添加到数据库
        UcenterMember registerMember = new UcenterMember();
        registerMember.setNickname(nickname);
        registerMember.setMobile(mobile);
        String md5Password = MD5.encrypt(password);//对密码进行加密
        registerMember.setPassword(md5Password);
        registerMember.setAvatar("https://lqq677.oss-cn-beijing.aliyuncs.com/2021/06/29/082e0c75cfc3456f8e5ecdf8991394cf.png");
        //插入数据
        int insert = baseMapper.insert(registerMember);
        if (insert == 1) {
            //删除redis中的验证码
            redisTemplate.delete(registerVo.getMobile());
        }
    }

    //更改密码
    @Override
    public void rePassword(RegisterVo registerVo) {
        //获取表单值
        String code = registerVo.getCode();//验证码
        String mobile = registerVo.getMobile();//手机号
        String password = registerVo.getPassword();//密码

        //非空判断
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(mobile)
                 || StringUtils.isEmpty(password)) {
            throw new GuliException(20001, "更改密码失败！");
        }

        //判断手机号是否存在，存在才可以改密码
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        UcenterMember member = baseMapper.selectOne(wrapper);
        if (member == null) {
            throw new GuliException(20001, "用户不存在,更改密码失败！");
        }

        //判断验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)) {
            throw  new GuliException(20001, "验证码错误,更改密码失败！");
        }

        //数据更改到数据库
        String md5Password = MD5.encrypt(password);//对密码进行加密
        member.setPassword(md5Password);
        int update = baseMapper.updateById(member);

        if (update == 1) {
            //删除redis中的验证码
            redisTemplate.delete(registerVo.getMobile());
        }
    }

    //统计某天注册人数
    @Override
    public Integer countRegister(String date) {
        Integer countRegister = baseMapper.countRegister(date);
        return countRegister;
    }

}
